﻿<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-show="orgType == 1" @click="opAdd">新增</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="opStore">设置绑定门店</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="opSupplier">设置绑定供应商</el-button>
                <!-- <el-button
                    type="info"
                    plain
                    icon="el-icon-delete"
                    v-show="orgType==1"
                    :disabled="!deleteState"
                    @click="opBatchDelete"
                >删除</el-button> -->
                <el-button type="primary" icon="el-icon-plus" v-show="orgType == 1" :disabled="!addState"
                    @click="opBankInfo">设置银行账户</el-button>
                <el-button type="primary" icon="el-icon-plus" v-show="orgType == 1" :disabled="!addState"
                    @click="opInvoiceInfo">设置开票信息</el-button>
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon class="vab-dropdown" :class="{ 'vab-dropdown-active': fold }" icon="arrow-up-s-line" />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px; margin-right: 20px">
                    <el-row type="flex" style="flex-wrap: wrap">
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="门店编号" label-width="100px" style="width: 100%">
                                <el-input placeholder="---请输入编号---" v-model="queryList.purchaseUnityCode"
                                    style="width: 100%" />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="采购单位简称" label-width="100px" style="width: 100%">
                                <el-input placeholder="---请输入采购单位简称---" v-model="queryList.purchaseUnityName"
                                    style="width: 100%" />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="采购单位简称" label-width="100px" style="width: 100%">
                                <el-input placeholder="---请输入采购单位简称---" v-model="queryList.purchaseUnityName"
                                    style="width: 100%" />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="采购单位全称" label-width="100px" style="width: 100%">
                                <el-input placeholder="---请输入采购单位全称---" v-model="queryList.purchaseUnityFullName"
                                    style="width: 100%" />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="状态" label-width="100px" style="width: 100%">
                                <el-select placeholder="---请选择状态---" v-model="queryList.status" style="width: 100%">
                                    <el-option label="---请选择---" value />
                                    <el-option v-for="item in optionsState" :key="item.value" :label="item.label"
                                        :value="item.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable ref="mytable" :queryType="'purchase-unity/pages'" :data="listData" :queryList="queryList"
                :column="columnList" :orderList="OrderList" :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange" row-key="purchaseUnityId" key="purchaseUnityId" stripe show-summary
                remoteSummary>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column prop="purchaseUnityCode" label="编号" width="120" sortable></el-table-column>
                <el-table-column prop="purchaseUnityName" label="采购单位简称" width="220" sortable>
                    <template #default="scope">
                        <span class="el-span-text" @click="opShow(scope.row, scope.$index)">{{
                            scope.row.purchaseUnityName }}</span></template>
                </el-table-column>
                <el-table-column prop="purchaseUnityFullName" label="采购单位全称" width="220" sortable></el-table-column>
                <el-table-column prop="realName" label="联系人" width="120" sortable></el-table-column>
                <el-table-column prop="mobile" label="联系电话" width="150" sortable></el-table-column>
                <el-table-column prop="address" label="地址" width="200" sortable show-overflow-tooltip></el-table-column>
                <el-table-column prop="crOrderName" label="制单人" width="150" sortable></el-table-column>
                <el-table-column prop="reconciliationName" label="对账人" width="150" sortable></el-table-column>
                <el-table-column prop="electronicAccount" label="电子账户" width="180" sortable></el-table-column>
                <el-table-column prop="balance" label="余额" width="120" sortable></el-table-column>
                <!-- <el-table-column label="是否自动接单" fixed="right" align="center" width="120" sortable>
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.isAutomaticReceive"
                            :active-value="true"
                            :inactive-value="false"
                            @change="setReceive(scope.row)"
                        />
                    </template>
                </el-table-column> -->
                <el-table-column prop="statusDesc" label="状态" width="150" sortable></el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="180">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small"
                                @click="opShow(scope.row, scope.$index)">查看</el-button>
                            <el-button text type="primary" size="small" v-if="orgType == 1"
                                @click="opEdit(scope.row, scope.$index)">编辑</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>

    <detail-dialog v-if="dialogDetail" ref="detailDialog" @fatherMethod="opBindData" @success="handleSaveSuccess"
        @closed="dialogDetail = false"></detail-dialog>

    <save-dialog v-if="dialogSave" ref="saveDialog" @fatherMethod="opBindData" @success="handleSaveSuccess"
        @closed="dialogSave = false"></save-dialog>

    <store-dialog v-if="dialogStore" ref="storeDialog" @fatherMethod="opBindData" @success="handleSaveSuccess"
        @closed="dialogStore = false"></store-dialog>

    <supplier-dialog v-if="dialogSupplier" ref="supplierDialog" @fatherMethod="opBindData" @success="handleSaveSuccess"
        @closed="dialogSupplier = false"></supplier-dialog>

    <bank-info-dialog v-if="dialogBankInfo" ref="bankInfoDialog" @fatherMethod="opBindData" @success="handleSaveSuccess"
        @closed="dialogBankInfo = false"></bank-info-dialog>
    <invoice-info-dialog v-if="dialogInvoiceInfo" ref="invoiceInfoDialog" @fatherMethod="opBindData"
        @success="handleSaveSuccess" @closed="dialogInvoiceInfo = false"></invoice-info-dialog>
</template>

<script>
import storeDialog from "./store";
import supplierDialog from "./supplier";
import detailDialog from "./detail";
import saveDialog from "./save";
import bankInfoDialog from "./bankInfo";
import invoiceInfoDialog from "./invoiceInfo";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "purchaseUnity",
    components: {
        detailDialog,
        saveDialog,
        publicjs,
        storeDialog,
        supplierDialog,
        bankInfoDialog,
        invoiceInfoDialog,
    },
    data() {
        return {
            dialogStore: false,
            dialogSupplier: false,
            dialogSave: false,
            dialogDetail: false,
            dialogBankInfo: false,
            dialogInvoiceInfo: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["purchaseUnityId"],
            columnList: [],
            OrderList: { purchaseUnityId: "Asc" },
            queryList: {},
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            orgCodeList: "",
            orgNameList: "",
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            orgType: 1,
        };
    },
    methods: {
        //查看
        opShow(row) {
            this.dialogDetail = true;
            this.$nextTick(() => {
                this.$refs.detailDialog.open("Show").setData(row);
            });
        },
        //添加
        opAdd(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add").setData({status:1,isAutomaticReceive:1});
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //设置是否自动接单
        async setReceive(row) { 
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post("purchase-unity/set-receive-status",
                    { purchaseUnityIds: [row.purchaseUnityId],isAutomaticReceive:row.isAutomaticReceive}
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //设置门店
        opStore() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要设置的集采中心");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("请选择一家集采中心");
                return;
            }

            this.dialogStore = true;
            this.$nextTick(() => {
                this.$refs.storeDialog.setData(this.selectionRows[0]);
                this.$refs.storeDialog.open("Edit");
            });
        },
        //设置供应商
        opSupplier() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要设置的集采中心");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("请选择一家集采中心");
                return;
            }

            this.dialogSupplier = true;
            this.$nextTick(() => {
                this.$refs.supplierDialog.setData(this.selectionRows[0]);
                this.$refs.supplierDialog.open("Edit");
            });
        },
        //设置银行账户
        opBankInfo() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要设置的采购单位");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("请选择一家采购单位");
                return;
            }
            this.dialogBankInfo = true;
            this.$nextTick(() => {
                this.$refs.bankInfoDialog.setData(this.selectionRows[0]);
                this.$refs.bankInfoDialog.open("Edit");
            });
        },
        //设置开票信息
        opInvoiceInfo() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要设置的采购单位");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("请选择一家采购单位");
                return;
            }
            this.dialogInvoiceInfo = true;
            this.$nextTick(() => {
                this.$refs.invoiceInfoDialog.setData(this.selectionRows[0]);
                this.$refs.invoiceInfoDialog.open("Edit");
            });
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {};
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status"] }
                );
                if (returnValue.status == 200) {
                    this.optionsState = returnValue.response.find(
                        (p) => p.code == "Status"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        this.orgType = tool.data.get("USER_INFO").AccountType;
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
